﻿
using DAL;

using DianShangHouTai.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace DianShangHouTai.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        [Authorize]
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 登陆页面地
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public ActionResult Login()
        {
            return View();
        }
        //
        [AllowAnonymous]
        public ActionResult Login(UILoginData data)
        {
           
            using (ShopContext db=new ShopContext())
            {
                var user = UserService.GetUser(data.username);
                if (user == null)
                {
                    return Json(new UIResult(false, "用户不存在"));
                }
                else
                {
                    if (user.PassWord == data.password)
                    {
                        FormsAuthentication.SetAuthCookie(data.username, false);
                        user.DengLuTime = DateTime.Now;
                        UserService.Save(user);
                        return Json(new UIResult(true, ""));
                    }
                    else
                    {
                        return Json(new UIResult(false, "密码错误"));
                    }
                }

            }
           
        }
    
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public ActionResult ChangePwd()
        {
          ViewBag.Name = User.Identity.Name;
          return View();       
        }
        [AllowAnonymous]
        [HttpPost]
        public ActionResult ChangePwd(UIChangePwd obj)
        {
           using(ShopContext db=new ShopContext())
            {
                if (ModelState.IsValid)
                {
                    User user = db.User.FirstOrDefault(p => p.ZhangHao == User.Identity.Name);
                    if (user != null)
                    {
                        if (user.PassWord == obj.oldpass)
                        {
                            user.PassWord = obj.newpass;
                            db.SaveChanges();
                            return Content("成功");
                        }
                        else
                        {
                            return Content("失败");
                        }
                       
                    }
                }
                else
                {
                    return Json(new UIResult(false, "确认密码和新密码必须一致"));
                }
               
            }
            return Json(new UIResult(false, "密码错误"));


        }
        /// <summary>
        /// 切换账号
        /// </summary>
        /// <returns></returns>
        public ActionResult SignOut()
        {
            FormsAuthentication.SignOut();
            //注销通行证
            return Redirect("~/Home/Login");
            
        }
        ShopContext db = new ShopContext();
        /// <summary>
        /// 产品的系统销量
        /// </summary>
        /// <returns></returns>
        public ActionResult Welcome()
        {
         
            return View();
        }
        /// <summary>
        /// 根据不同用户管理不同的权限
        /// </summary>
        /// <returns></returns>
       
        [HttpPost]
        public ActionResult GetModule()
        {
            List<V_CUserModules> list = CUserModuleService.GetList(p => p.ZhangHao == User.Identity.Name);
            StringBuilder stingbuilder = new StringBuilder();
            List<V_CUserModules> parentlist = list.Where(p => p.MParentId == 0).ToList();
            foreach (var item in parentlist)
            {
                List<V_CUserModules> Childlist = list.Where(p => p.MParentId == item.MId).ToList();
                if (Childlist.Count > 0)
                {
                    stingbuilder.Append("<li class='layui-nav-item layui-nav-itemed'>");
                    stingbuilder.Append("<a class='' href='javascript:;'>" + item.MModuleName + "</a>");
                    stingbuilder.Append("<dl class='layui-nav-child'>");
                    foreach (var ChildModule in Childlist)
                    {
                        string url = "/" + ChildModule.MControllerName + "/" + ChildModule.MActionName;
                        stingbuilder.Append("<dd><a href='javascript:;' _href='" + url + "'>" + ChildModule.MModuleName + "</a></dd>");
                    }
                    stingbuilder.Append("</dl></li>");
                }
            }
            return Content(stingbuilder.ToString());
        }
      
    }
}